<template>
  <div>
    <div style="padding:20px 10px; border-bottom:1px solid #ebebeb">
      <span>校区管理员列表</span>
    </div>
    <el-col class="add-btn" :span="4">
      <el-button type="primary" @click="centerDialogVisible1 = true">新增</el-button>
    </el-col>
    <template>
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column type="index" :index="indexMethod" align="center"></el-table-column>
        <el-table-column label="姓名" prop="username" align="center"></el-table-column>
        <el-table-column label="性别" prop="gender" align="center"></el-table-column>
        <el-table-column label="电话" prop="mobile" align="center"></el-table-column>
        <el-table-column label="所属校区" prop="school.school_name" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <!-- <template slot="header">
            <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
          </template>-->
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row);centerDialogVisible = true">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 编辑弹框 -->
    <el-dialog title="编辑信息" :visible.sync="centerDialogVisible" width="30%" center>
      <div class="form-list">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>

          <el-form-item label="电话">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>

          <el-form-item label="校区">
            <el-select v-model="form.school" placeholder="校区选择" style="display:block">
              <el-option
                v-for="item in schoolData"
                :key="item.id"
                :label="item.school_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit(form); centerDialogVisible = false">提交</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹框 -->
    <el-dialog title="编辑信息" :visible.sync="centerDialogVisible1" width="30%" center>
      <div class="form-list">
        <el-form ref="form2" status-icon :model="form2" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="form2.username" placeholder="请输入姓名"></el-input>
          </el-form-item>

          <el-form-item label="电话">
            <el-input v-model="form2.mobile" placeholder="请输入11位手机号"></el-input>
          </el-form-item>

          <el-form-item label="密码">
            <el-input type="password" v-model="form2.pwd" placeholder="密码建议手机后六位"></el-input>
          </el-form-item>

          <el-form-item label="校区">
            <el-select v-model="form2.school" placeholder="校区选择" style="display:block">
              <el-option
                v-for="item in schoolData"
                :key="item.id"
                :label="item.school_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="addUserBtn(); centerDialogVisible1 = false">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "user",
  data() {
    return {
      // 新增弹框默认状态
      centerDialogVisible: false,
      centerDialogVisible1: false,
      schoolData: [],
      tableData: [],
      // 编辑列表信息
      form: {
        username: "",
        mobile: "",
        school: "",
        id: ""
      },
      // 新增列表信息
      form2: {
        username: "",
        pwd: "",
        mobile: "",
        school: ""
      }
    };
  },

  mounted: function() {
    this.userListInfo();
    this.schoolInfo();
  },

  methods: {
    // 获取用户列表
    userListInfo() {
      let that = this;
      that.$api.getUserListInfo().then(res => {
        // console.log(res.data);
        that.tableData = res.data.results;
      });
    },

    // 获取校区列表
    schoolInfo() {
      let that = this;
      let params_data = {};
      if (!that.$store.state.is_superuser) {
        params_data = {
          area: that.$store.state.area_id
        };
      }
      that.$api
        .getSchoolListInfo({
          params: { params_data }
        })
        .then(res => {
          that.schoolData = res.data;
          // console.log(that.schoolData);
        })
    },

    // ==============================================================

    //   编辑用户-获取行数据
    handleEdit(row) {
      // console.log(row,'....') ;
      let that = this;
      that.form.username = row.username;
      that.form.mobile = row.mobile;
      that.form.school = row.school.school_name;
      that.form.id = row.id;
    },

    // 编辑用户-数据提交
    onSubmit(form) {
      let that = this;
      // console.log(that.form,'./.')
      let postData = this.$qs.stringify({
        username: that.form.username,
        mobile: that.form.mobile,
        id: that.form.id
      });
      that.$api.userListItemInfo(form.id, postData).then(res => {
        // console.log(res);
        that.tableData.forEach(item => {
          if (item.id === form.id) {
            item.username = res.data.username;
            item.mobile = res.data.mobile;
          }
        });
      });
    },

    // ===========================================================

    // 新增数据提交
    addUserBtn() {
      let that = this;
      let PostData = this.$qs.stringify({
        username: that.form2.username,
        mobile: that.form2.mobile,
        school: that.form2.school,
        password: that.form2.pwd,
        is_active: 1
      });
      that.$api
        .addUserInfo(PostData)
        .then(res => {
          that.userListInfo();
          that.$message({
            type: "success",
            message: "新增成功!"
          });
        })
    },

    // =============================================================

    //   删除按钮
    handleDelete(obj) {
      // console.log(obj);
      let that = this;
      that.$api.DeletUserInfo(obj.id, { is_active: 0 }).then(res => {
        // console.log(res)
        if (res.status == 200) {
          that.userListInfo();
          that.$message({
            type: "success",
            message: "删除成功!"
          });
        }
      });
    },

    // 索引排序
    indexMethod(index) {
      return index * 1;
    }
  }
};
</script>

<style scoped>
.add-btn {
  margin: 30px 30px 30px 10px;
  padding-right: 120px;
}
</style>